<template>
  <div class="register">
    <div class="box">
      <!-- 传一个引用 -->
      <i class="el-icon-close" @click="close_registers"></i>
      <div class="content">
        <div class="nav">
          <span>账号注册</span>
        </div>
        <!-- @blur鼠标移开输入框时走的事件 -->
        <el-input
          placeholder="用户名"
          prefix-icon="el-icon-user"
          v-model="username"
          clearable
          @blur="check_usernames"
        ></el-input>
        <el-input
          placeholder="手机号"
          prefix-icon="el-icon-phone"
          v-model="mobile"
          clearable
          @blur="check_mobile"
        ></el-input>
        <el-input
          placeholder="密码"
          prefix-icon="el-icon-key"
          v-model="password"
          clearable
          show-password
          @blur="check_password"
        ></el-input>
        <el-input
          placeholder="再次输入密码"
          prefix-icon="el-icon-key"
          v-model="second_password"
          clearable
          show-password
          @blur="check_second_password"
        ></el-input>
        <el-button type="primary" @click="register">注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      username: "",
      mobile: "",
      password: "",
      second_password: ""
    };
  },
  methods: {
    close_registers() {
      this.$emit("close");
    },
    register() {
      if (
        !this.username ||
        !this.password ||
        !this.mobile ||
        !this.second_password
      )
        return this.$notify({
          title: "请输入账号或密码或手机号",
          type: "error",
          duration: 1500
        });
      if (this.password != this.second_password)
        return this.$notify({
          title: "两次密码不一致",
          type: "error",
          duration: 1500,
          onClose: () => {
            this.password = "";
            this.second_password = "";
          }
        });
      this.$axios({
        url: this.$settings.base_url + "/user/register/",
        method: "post",
        data: {
          username: this.username,
          password: this.password,
          mobile: this.mobile,
          second_password: this.second_password
        }
      })
        .then(response => {
          // 弹出框提示后，关闭注册界面
          this.$notify({
            title: "注册成功",
            type: "success",
            duration: 500,
            // #结束事件
            onClose: () => {
              this.$emit("success");
            }
          });
        })
        .catch(() => {
          // 异常
          this.$notify({
            title: "注册失败,格式异常",
            type: "error",
            duration: 1500,
            // 清除账号密码框内容
            onClose: () => {
              this.username = "";
              this.password = "";
              this.mobile = "";
              this.second_password = "";
            }
          });
        });
    },
    check_usernames() {
      if (this.username.length < 8 || this.username.length > 16)
        return this.$notify({
          title: "账号必须是8至16位",
          type: "error",
          duration: 1000,
          onClose: () => {
            this.username = "";
          }
        });
      if (this.username.match(/^[A-Za-z]/) == null)
        return this.$notify({
          title: "账号必须以大小写字母为开头",
          type: "error",
          duration: 1000,
          onClose: () => {
            this.username = "";
          }
        });
      this.$axios({
        url: this.$settings.base_url + "/user/verify_username/",
        method: "get",
        params: {
          username: this.username
        }
      })
        .then(response => {
          // 如果状态码恒等于2则报warning
          if (response.data.status === 2) {
            // 警告窗方法
            this.$notify({
              title: response.data.msg,
              type: "warning",
              // 警告框停留世间
              duration: 1000,
              onClose: () => {
                this.username = "";
              }
            });
          }
        })
        .catch(error => {
          //如果接口状态码不是200则返回以下错误
          this.$notify({
            title: error.response.data.msg,
            type: "error",
            duration: 1000,
            onClose: () => {
              this.username = "";
            }
          });
        });
    },
    check_mobile() {
      if (this.mobile.match(/^1[3-9][0-9]{9}$/) == null)
        return this.$notify({
          title: "手机号格式有误",
          type: "error",
          duration: 1000,
          onClose: () => {
            this.mobile = "";
          }
        });
      this.$axios({
        url: this.$settings.base_url + "/user/verify_mobile/",
        method: "get",
        params: {
          mobile: this.mobile
        }
      })
        .then(response => {
          // 如果状态码恒等于2则报warning
          if (response.data.status === 2) {
            // 警告窗方法
            this.$notify({
              title: response.data.msg,
              type: "warning",
              // 警告框停留世间
              duration: 1000,
              onClose: () => {
                this.mobile = "";
              }
            });
          }
        })
        .catch(error => {
          //如果接口状态码不是200则返回以下错误
          this.$notify({
            title: error.response.data.msg,
            type: "error",
            duration: 1000,
            onClose: () => {
              this.mobile = "";
            }
          });
        });
    },
    check_password() {
      if (this.password.length < 8 || this.password.length > 16)
        return this.$notify({
          title: "密码必须是8至16位",
          type: "error",
          duration: 1000,
          onClose: () => {
            this.password = "";
          }
        });
    },
    check_second_password() {
      if (this.second_password.length < 8 || this.second_password.length > 16)
        return this.$notify({
          title: "密码必须是8至16位",
          type: "error",
          duration: 1000,
          onClose: () => {
            this.second_password = "";
          }
        });
    }
  }
};
</script>

<style scoped>
.register {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
  background-color: rgba(0, 0, 0, 0.3);
}

.box {
  width: 400px;
  height: 420px;
  background-color: white;
  border-radius: 10px;
  position: relative;
  top: calc(50vh - 210px);
  left: calc(50vw - 200px);
}

.el-icon-close {
  position: absolute;
  font-weight: bold;
  font-size: 20px;
  top: 10px;
  right: 10px;
  cursor: pointer;
}

.el-icon-close:hover {
  color: darkred;
}

.content {
  position: absolute;
  top: 40px;
  width: 280px;
  left: 60px;
}

.nav {
  font-size: 20px;
  height: 38px;
  border-bottom: 2px solid darkgrey;
}

.nav > span {
  margin: 0 0 0 100px;
  color: black;
  user-select: none;
  cursor: pointer;
  font: 20px Extra large;
  font-family: "Microsoft YaHei";
}

.el-input,
.el-button {
  margin-top: 10px;
  font: 13px Small;
  font-family: "Microsoft YaHei";
}

.el-button {
  width: 100%;
  font-size: 18px;
}
</style>